/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dancrawler.db;

import dancrawler.common.Misc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URLDecoder;
import java.nio.channels.FileChannel;

/**
 *
 * @author Team 4 RMI, HINGDA10
 */
public class dbCopyTest {

  private String path;
  private String fileTo;
  private String fileFrom;

  /**
   * Copies database to %APPDATA%\dancrawler (Windows) or HOME/dancrawler (*N*X)
   */
  public dbCopyTest() {
    try {
    path = Misc.getAppdataPath("");

    fileFrom = URLDecoder.decode(this.getClass().getResource("/res/db/dancrawler.s3db").toString(), "UTF-8").replace("file:/", "");
    fileTo = path + File.separatorChar + "dancrawler.s3db";

    File dbSource = new File(fileFrom);
    File dbDestination = new File(fileTo);
    if (!dbDestination.exists()) {
      dbDestination.createNewFile();
    }


    try (
            FileChannel in = new FileInputStream(dbSource).getChannel();
            FileChannel out = new FileOutputStream(dbDestination).getChannel()) {

      out.transferFrom(in, 0, in.size());
    }
    } catch (Exception e) {}
    System.out.println(path);
  }

  /**
   *
   * @param args
   */
  public static void main(String[] args) {
    dbCopyTest dt = new dbCopyTest();
  }
}
